package com.oyc.demo.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.fastjson.JSONObject;
import com.oyc.demo.domain.Initdata;
import com.oyc.demo.listener.InitdataListener;
import com.oyc.demo.service.InitdataService;
import com.oyc.demo.service.UserService;
import com.oyc.demo.util.Constants;
import com.oyc.demo.util.Result;

import io.swagger.annotations.ApiOperation;

/**
 * (Initdata)表控制层
 *
 * @author 杜学森
 * @since 2021-01-20 18:51:53
 */
@RestController
@RequestMapping("/initdata")
public class InitdataController {
	/**
	 * 服务对象
	 */
	@Autowired
	private InitdataService initdataService;

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/importexcel", method = RequestMethod.POST)
	@ApiOperation("导入")
	public Result<JSONObject> importexcel(@RequestParam("provinceName") String provinceName,
			@RequestParam("file") MultipartFile file) {
		try {
			if (file.isEmpty()) {
				return Result.error("导入失败,请上传文件", -1);
			}

			if (!file.getOriginalFilename().toLowerCase().endsWith(".xls")
					&& !file.getOriginalFilename().toLowerCase().endsWith(".xlsx")) {
				return Result.error("导入失败,请导入正确的excel文件", -1);
			}

			String fileName = "";
			// 可以远程上传文件
			if (Constants.debugmodel) {
				// 判断上传路径是否存在
				File savefile = new File(Constants.importpath);
				if (!savefile.exists()) {
					savefile.mkdirs();
				}
				// 生成随机文件名
				fileName = Constants.importpath + UUID.randomUUID().toString() + ".xlsx";
				File dest = new File(fileName);
				file.transferTo(dest);
			} else {
				fileName = file.getOriginalFilename();
			}


			
			// 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
			EasyExcel.read(fileName, Initdata.class, new InitdataListener(initdataService, provinceName)).sheet().headRowNumber(3).doRead();

			JSONObject obj = initdataService.calculate(provinceName, "cablebasic"); 

			return Result.success(obj);

		} catch (ExcelAnalysisException e) {
			return Result.error("导入失败,没有正确的模板文件,请选择正确的模板文件后再执行导入操作", 500);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.error("导入失败", 500);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.error("导入失败", 500);
		} 

	}

	@RequestMapping(value = "/importexcelclient", method = RequestMethod.POST)
	@ApiOperation("导入")
	public Result<JSONObject> importexcelclient(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		// 获取用户默认省市
		String provinceName = userService.findprovincename(null);
		try {

			if (provinceName==null||provinceName.trim().length()==0) {
				return Result.error("请登录后再操作", -1);
			}

			if (file.isEmpty()) {
				return Result.error("导入失败,请上传文件", -1);
			}

			if (!file.getOriginalFilename().toLowerCase().endsWith(".xls")
					&& !file.getOriginalFilename().toLowerCase().endsWith(".xlsx")) {
				return Result.error("导入失败,请导入正确的excel文件", -1);
			}

			String fileName = "";
			// 可以远程上传文件
			if (Constants.debugmodel) {
				// 判断上传路径是否存在
				File savefile = new File(Constants.importpath);
				if (!savefile.exists()) {
					savefile.mkdirs();
				}
				// 生成随机文件名
				fileName = Constants.importpath + UUID.randomUUID().toString() + ".xlsx";
				File dest = new File(fileName);
				file.transferTo(dest);
			} else {
				fileName = file.getOriginalFilename();
			}

			// 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
			EasyExcel.read(fileName, Initdata.class, new InitdataListener(initdataService, provinceName)).sheet()
					.headRowNumber(3).doRead();

			JSONObject obj = initdataService.calculate(provinceName, "client");

			return Result.success(obj);

		} catch (ExcelAnalysisException e) {
			return Result.error("导入失败,没有正确的模板文件,请选择正确的模板文件后再执行导入操作", 500);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.error("导入失败", 500);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.error("导入失败", 500);
		} 

	}

}